package ro.isdc.vms.vehicleadvertisement.service.impl;

import java.util.List;

import ro.isdc.vms.vehicleadvertisement.dao.VehicleAdvertisementDAO;
import ro.isdc.vms.vehicleadvertisement.domain.VehicleAdvertisement;
import ro.isdc.vms.vehicleadvertisement.service.VehicleAdvertisementService;

/**
 * Vehicle Advertisement Business Component Services.
 * 
 * @author Andrei Chirila / ISDC! Romania
 * @version 1.0.0
 * @date Jul 21, 2009
 */
public class VehicleAdvertisementServiceImpl implements
		VehicleAdvertisementService {

  /**
   * The vehicle advertisement DAO reference.
   */
	private VehicleAdvertisementDAO vehicleAdvertisementDAO;
	
	/**
	 * The old flagged ads cache.
	 */
	private List<VehicleAdvertisement> flaggedOldAdvetisementsCache;
	
	/**
	 * @param vehicleAdvertisementDAO the vehicleAdvertisementDAO to set
	 */
	public void setVehicleAdvertisementDAO(
			VehicleAdvertisementDAO vehicleAdvertisementDAO) {
		this.vehicleAdvertisementDAO = vehicleAdvertisementDAO;
	}


	/**
	 * {@inheritDoc}
	 */
	public VehicleAdvertisement findNextOldFlaggedVehicleAdvertisement() {
		if (flaggedOldAdvetisementsCache == null) {
			flaggedOldAdvetisementsCache = vehicleAdvertisementDAO.findOldFlaggedAdvertisements();
		}
		
		if (!flaggedOldAdvetisementsCache.isEmpty()) {
			return flaggedOldAdvetisementsCache.remove(0);
		}
		
		flaggedOldAdvetisementsCache = null;
		return null;
	}

}
